#include "TxtReader.h"
#include "../Model/Model.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

void TxtReader::makeBook(std::string & path, int lines, Model & m) {
	
	//int MAX_LENGTH = 20; // Как передать сюда из View? 
	std::ifstream book(path.c_str());
	//m.setLinesRed(0);
		
	int indP = 0;	
	while (!book.eof()) {
		std::string buf;
		getline(book, buf);
				
		for (size_t i = 0; i < buf.length() ; ++i) {
			if (buf[i] == '.' || buf[i] == '!' || buf[i] == '?')
				++indP;
		}
		 
		m.addText(buf);	
		//std::cout << indP << std::endl;	
		m.addPointIndex(indP);
	}
	m.setLinesRed(lines);
	

}

void TxtReader::makeBookByString(std::string & path, Model & m) {
 
	std::ifstream book(path.c_str());
	//m.setLinesRed(0);
		
	std::string prevSentence = "";
	while (!book.eof()) {
		std::string buf;
		int prevSentenceEnd = 0;	
		getline(book, buf);
	
		if (prevSentence.compare("")) { 
			buf = prevSentence + buf;
		}
				
		for (size_t i = 0; i < buf.length() ; ++i) {
			if (buf[i] == '.' || buf[i] == '!' || buf[i] == '?') {
				m.addText(buf.substr(prevSentenceEnd, i - prevSentenceEnd));
				prevSentenceEnd = i + 1;
			}
		}
				
	}
	m.setLinesRed(0);
	

}

/*
std::wstring TxtReader::fromSimpleString(const std::string & utf8s) {

	if (utf8s.empty())
		return std::wstring();

	int wcharcount = MultiByteToWideChar(CP_UTF8, 0, utf8s.data(), (int) utf8s.size(), 					NULL, 0);

	if (wcharcount < 0)
		return std::wstring();

	std::wstring buffer;
	buffer.resize(wcharcount);
	MultiByteToWideChar(CP_UTF8, 0, utf8s.data(), (int) utf8s.size(), &(buffer.front()), wcharcount);

	return buffer;

}
*/
